package alogrithm;

/**
 * @author lbw
 * @version 1.0
 * @date 2021/10/18 20:05
 */


public class ReverseBetween {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        // write code here
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for (int i = 0; i < m - 1; ++i) {
            pre = pre.next;
        }
        ListNode cur = pre.next;
        ListNode next;
        for (int i = 0; i < n - m; ++i) {
            next = cur.next;
            cur.next = next.next;

            next.next = pre.next;
            pre.next = next;
        }
        return dummyNode.next;
    }

    public class ListNode {
        int val;

        ListNode next = null;

        public ListNode(int val) {
            this.val = val;
        }
    }
}
